﻿using System.Collections.ObjectModel;
using ColladaDotNet;
using contentviewer.Model;
using GalaSoft.MvvmLight;

namespace contentviewer.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class ColladaTreeViewModel : ViewModelBase
    {
        private readonly IDataService _dataService;

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        ///         
        private ObservableCollection<ColladaNodeViewModel> _colladaFiles;

        public ColladaTreeViewModel(IDataService dataService)
        {
            COLLADA d = null;
            _dataService = dataService;
            _dataService.GetData(
                (item, error) =>
                {
                    if (error != null)
                    {
                        // Report error here
                        return;
                    }
                    d = item;
                });

            _colladaFiles = new ObservableCollection<ColladaNodeViewModel>();
            _colladaFiles.Add(new ColladaNodeViewModel(d, null, null));

			if (IsInDesignMode)
				ExpandTree(_colladaFiles[0], "asset", typeof(AssetViewModel));
        }

		private void ExpandTree(NodeViewModel treeItemVM, string id, System.Type type)
		{
			treeItemVM.IsExpanded = true;
			if (treeItemVM.GetType().Equals(type) && treeItemVM.ToString().Contains(id))
			{
				treeItemVM.IsSelected = true;
				id = "dont select";
			}
			foreach (var child in treeItemVM.Children)
			{
				ExpandTree(child, id, type);
			}
		}

        public ObservableCollection<ColladaNodeViewModel> TreeItems
        {
            get { return _colladaFiles; }
        }
    }
}